<?php
require_once(dirname(__FILE__).'/../config.php');
$etime = intval($_GET['etime']);
$tz = trim($_REQUEST['tzde']);		
if(!empty($tz)){
	$back_act = urldecode($tz);
}else{
    $back_act='';
}
$wx_title = "网页授权登录确认";
$wx_btn = "dl";
//判断二维码是否过期
$daytime = time();
$endtime = $daytime-$etime;
if($endtime >= 600)
{
   $wx_title = "二维码已过期，请重新获取!";
   $wx_btn = "dn";
}
else
{
	//判断二维码是否已登录
	$sq = $dsql->GetOne("select * from `#@__wxlogin_ewm` where time='$etime'");
	if($sq['openid'])
	{
		$wx_title = "二维码已失效，请重新获取!";
		$wx_btn = "dn";
	}
	else
	{
		require_once('weixin.class.php');
		$weixin = new class_weixin();

		if(!isset($_GET["code"]))
		{
			$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_userinfo", "123");
			Header("Location: $jumpurl");
		}
		else
		{
			$access_token_oauth2 = $weixin->oauth2_access_token($_GET["code"]);
			$userinfo = $weixin->get_user_info($access_token_oauth2['openid']); //需关注获取方法
			if($userinfo["subscribe"]==0)
			{
				$userinfo = $weixin->oauth2_get_user_info($access_token_oauth2['access_token'], $access_token_oauth2['openid']);
			}
		}

		$openid			=	$userinfo["openid"];
		$logincook		=	315360000;
		$userpic		=	$userinfo['headimgurl'];//头像
		$usernamez		=	$userinfo["nickname"];
		$username		=	$weixin->filter($usernamez);
		$address		=	"".$userinfo['country']."".$userinfo['province']."".$userinfo['city']."";//用户地区
		$gzzt			=	$userinfo["subscribe"];//关注状态
	}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>微信扫码授权</title>
<meta name="format-detection" content="telephone=no">  
<meta name="apple-mobile-web-app-capable" content="yes">  
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport">
<link rel="stylesheet" type="text/css" href="/oauth/wechat/css/wxlogin.css">
<script src="/oauth/wechat/js/jquery.js"></script>
</head>
<body bgcolor="#fff"> 
<section classid="wxlogin_sq">
<div class="wxsq_bj"><img src="/oauth/wechat/css/img/wxlogin_icon.png"></div>
<div class="wxsq_title"><?php echo $wx_title;?></div>
<div class="wxsq_btn <?php echo $wx_btn;?>"><a href="javascript:void(0);" onClick="wxsq();">确认登录</a></div>
</section>
<input type="hidden" id="tzde" value="<?php echo $back_act;?>" />
<input type="hidden" id="time" value="<?php echo $etime;?>" />
<input type="hidden" id="openid" value="<?php echo $openid;?>" />
<input type="hidden" id="username" value="<?php echo $username;?>" />
<input type="hidden" id="userpic" value="<?php echo $userpic;?>" />
<input type="hidden" id="address" value="<?php echo $address;?>" />
<input type="hidden" id="gzzt" value="<?php echo $gzzt;?>" />
<script>var domain = '<?php echo $cfg_basehost; ?>';</script>
<script src="/oauth/wechat/js/wxlogin.js"></script>
</body>
</html>